import 'dart:io';
import 'package:dio/dio.dart';

class BaseResp<T> {
  T data;

  HttpHeaders headers;

  RequestOptions request;

  int statusCode;

  String statusMessage;

  List<RedirectInfo> redirects;

  BaseResp(this.headers, this.request, this.redirects, this.statusMessage,
      this.data, this.statusCode);

  @override
  String toString() {
    StringBuffer sb = new StringBuffer('{');
    sb.write(",\"code\":$statusCode");
    sb.write(",\"msg\":\"$statusMessage\"");
    sb.write(",\"data\":\"$data\"");
    sb.write('}');
    return sb.toString();
  }
}

//网络访问工具
class HttpUtils {
  static HttpUtils _httpUtils = HttpUtils._internal();
  static Dio _dio;
  static BaseOptions _baseOptions = getBaseOptions();
  factory HttpUtils() => _httpUtils;
  HttpUtils._internal() {
    _dio = new Dio(_baseOptions);
  }
  //添加拦截器
  void addInterceptor(Interceptor interceptor) {
    _dio.interceptors.add(interceptor);
  }

  void addInterceptors(List<Interceptor> interceptors) {
    interceptors.forEach((interceptor) {
      _dio.interceptors.add(interceptor);
    });
  }

  //request
  Future<BaseResp> request(
    String path, {
    data,
    Map<String, dynamic> queryParameters,
    CancelToken cancelToken,
    Options options,
    ProgressCallback onSendProgress,
    ProgressCallback onReceiveProgress,
  }) async {
    Response response;
    try {
      response = await _dio.request(
        path,
        data: data,
        queryParameters: queryParameters,
        cancelToken: cancelToken,
        onSendProgress: onSendProgress,
        onReceiveProgress: onReceiveProgress,
        options: options,
      );
    } catch (e) {
      return new Future.error(new DioError(
        response: response,
        message: "statusCode: $response.statusCode, service error",
        type: DioErrorType.RESPONSE,
      ));
    }
    return BaseResp(response.headers, response.request, response.redirects,
        response.statusMessage, response.data, response.statusCode);
  }

  //get
  Future<BaseResp> get(
    String path, {
    Map<String, dynamic> queryParameters,
    Options options,
    CancelToken cancelToken,
    ProgressCallback onReceiveProgress,
  }) async {
    return await request(path,
        queryParameters: queryParameters,
        options: _checkOptions("GET", options),
        cancelToken: cancelToken,
        onReceiveProgress: onReceiveProgress);
  }

//post
  Future<BaseResp> post(
    String path, {
    data,
    Map<String, dynamic> queryParameters,
    Options options,
    CancelToken cancelToken,
    ProgressCallback onSendProgress,
    ProgressCallback onReceiveProgress,
  }) async {
    return await request(path,
        data: data,
        queryParameters: queryParameters,
        options: _checkOptions("POST", options),
        cancelToken: cancelToken,
        onReceiveProgress: onReceiveProgress,
        onSendProgress: onReceiveProgress);
  }

  //新建一个dio实例
  static Dio createDio({BaseOptions options}) {
    new Dio(options);
  }

  static BaseOptions getBaseOptions() {
    return BaseOptions(
      baseUrl: "",
      connectTimeout: 10000,
      receiveTimeout: 10000,
    );
  }

  Options _checkOptions(method, options) {
    if (options == null) {
      options = new Options();
    }
    options.method = method;
    return options;
  }
}
